import time

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
# from ProcessManager import ProcessManager
from loguru import logger

# from Boiler import Boiler
import Cmd
from DiyArm import DiyArmInst
from Boiler import Boiler
from Config import Config
from ConveyorBelt import ConveyorManager
from ProcessManager import ProcessManager
from widgets.ConveyorWidget import ConveyorWidget


class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.counter = 0

        self.mutex = QMutex()

        self.bot_running = False
        self.get_box_queue = []
        self.pour_queue = []

        layout = QVBoxLayout()

        # self.temp_label = QLabel("0")
        self.track_label = QLabel("0")
        self.task_label = QLabel("")
        self.running_label = QLabel("")

        form_layout = QFormLayout()
        # form_layout.addRow(QLabel("水温:"), self.temp_label)
        form_layout.addRow(QLabel("导轨位置:"), self.track_label)
        form_layout.addRow(QLabel("任务数量:"), self.task_label)
        form_layout.addRow(QLabel("正在执行:"), self.running_label)

        ProcessManager().sig_track_pos.connect(lambda p: self.track_label.setText(str(p)))
        ProcessManager().sig_queue_sum.connect(lambda s: self.task_label.setText(str(s)))
        # boiler
        boiler_form_layout = QFormLayout()
        self.boiler_temp_label = QLabel()
        self.boiler_hp_checkbox = QCheckBox()
        self.boiler_switch_checkbox = QCheckBox()
        self.boiler_state_label = QLabel()
        ProcessManager().sig_temp.connect(lambda t: self.boiler_temp_label.setText(str(t)))
        self.boiler_hp_checkbox.toggled.connect(lambda state: Boiler().set_heat_preservation(state))
        self.boiler_switch_checkbox.toggled.connect(lambda state: Boiler().turn(state))
        Boiler().sig_switch.connect(lambda state: self.boiler_state_label.setText(str(state)))

        boiler_form_layout.addRow(QLabel("炉子温度"), self.boiler_temp_label)
        boiler_form_layout.addRow(QLabel("炉子保温"), self.boiler_hp_checkbox)
        boiler_form_layout.addRow(QLabel("炉子开关"), self.boiler_switch_checkbox)
        boiler_form_layout.addRow(QLabel("炉子状态"), self.boiler_state_label)

        self.log_text_edit = QTextEdit()
        ProcessManager().sig_msg_report.connect(lambda msg: self.log_text_edit.append(
            "{}:{}".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), msg)))

        ProcessManager().sig_home_report.connect(lambda msg: self.log_text_edit.append(
            "{}:{}".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), msg[1])))

        # 自定义widget
        conveyor_wid = ConveyorWidget(ConveyorManager().count())
        ConveyorManager().sig_sensor_state.connect(conveyor_wid.update_widget)

        self.l = QLabel("Start")
        # b = QPushButton("DANGER!")
        # b.pressed.connect(self.oh_no)

        addAction = QPushButton("增加")
        addAction.pressed.connect(self.add_action)

        homeAction = QPushButton("初始化")
        homeAction.pressed.connect(self.home_action)

        stop_action_btn = QPushButton("停止")
        # stop_action_btn.pressed.connect(self.stop_action)

        layout.addLayout(form_layout)
        layout.addWidget(self.l)
        layout.addWidget(homeAction)
        layout.addWidget(addAction)
        layout.addWidget(stop_action_btn)

        layout.addLayout(boiler_form_layout)
        layout.addWidget(conveyor_wid)
        layout.addWidget(self.log_text_edit)


        self.line_edit = QLineEdit()
        layout.addWidget(self.line_edit)

        home_btn = QPushButton("home")
        home_btn.pressed.connect(self.on_home_btn_pressed)
        layout.addWidget(home_btn)

        cmd_btn = QPushButton("run")
        cmd_btn.pressed.connect(self.on_cmd_btn_pressed)
        layout.addWidget(cmd_btn)

        w = QWidget()
        w.setLayout(layout)

        self.setCentralWidget(w)

        self.show()

        ProcessManager().stat_signal.connect(lambda pos: self.l.setText(str(pos)))
        # Track().pos_signal.connect(lambda pos: self.l.setText(str(pos)))

        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.recurring_timer)

        # self.closeEvent.co
        # self.timer.start()

    def on_home_btn_pressed(self):
        # print(self.line_edit)
        cmd = self.line_edit.text()
        

    def on_cmd_btn_pressed(self):
        # print(self.line_edit)
        cmds = Cmd.load_from_file("./cmds/action_pour")
        logger.debug(cmds)
        DiyArmInst().add_cmd("DiyArm1", cmds)



    def recurring_timer(self):
        self.counter += 1
        self.l.setText("Counter: %d" % self.counter)

    def add_action(self):
        ProcessManager().add_action()
        self.log_text_edit.append("add_action")

    def home_action(self):
        logger.info("home action")
        ProcessManager().devices_home()

    def closeEvent(self, event):
        logger.debug("closeEvent")
        DiyArmInst().close()
        Boiler().close()


app = QApplication([])
window = MainWindow()
app.exec_()
